package com.jbh.wanandroidkotlin.activity

import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.google.gson.Gson
import com.jbh.apilibrary.AppData
import com.jbh.apilibrary.bean.IndexArticleListBean
import com.jbh.apilibrary.bean.IndexArticleListResult
import com.jbh.apilibrary.i.ApiCallBack
import com.jbh.wanandroidkotlin.R
import com.jbh.wanandroidkotlin.util.WEB_LINK
import com.jbh.wanandroidkotlin.util.data
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.footer.ClassicsFooter
import com.scwang.smartrefresh.layout.header.ClassicsHeader
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener
import com.scwang.smartrefresh.layout.listener.OnRefreshListener
import kotlinx.android.synthetic.main.common_list_layout.*
import kotlinx.android.synthetic.main.common_list_layout.mRecyclerView
import kotlinx.android.synthetic.main.common_list_layout.mSmartRefreshLayout
import kotlinx.android.synthetic.main.fragment_index.*

class ArticleListActivity : BaseActivity() {

    /**
     * 记录上拉还是下拉
     */
    private var isPullRefresh = true
    /**
     * 当前页码
     */
    private var page = 0
    /**
     * 文章的总页数
     *
     */
    private var pageAll = 0;
    /**
     * 文章列表
     *
     */
    private val listContent  = mutableListOf<IndexArticleListBean>()

    private var id = 0

   private var contentListAdapter:ContentListAdapter?=null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_article_list)
        id = intent.getIntExtra("id",0)

        initRefresh()
    }

    private fun initRefresh(){
        mRecyclerView.apply {
            val l = LinearLayoutManager(this@ArticleListActivity)
            l.orientation = LinearLayoutManager.VERTICAL
            layoutManager = l
            contentListAdapter = ContentListAdapter(listContent)
            adapter = contentListAdapter
        }
        contentListAdapter?.setOnItemClickListener { adapter, view, position ->
            val b = Bundle();
            b.putString(WEB_LINK,listContent[position].link)
            startActivity(Intent(this,CommonActivity::class.java).putExtra(data, b))
        }

        mSmartRefreshLayout?.apply {
            autoRefresh()
            setEnableRefresh(true)
            setEnableLoadMore(true)
            setOnRefreshListener(RefreshHead())
            setOnLoadMoreListener(RefreshFoot())
        }
    }



    inner class RefreshFoot : OnLoadMoreListener {

        override fun onLoadMore(refreshLayout: RefreshLayout) {
            isPullRefresh = false
            page ++ ;
            if (pageAll >= page){
                mainTreeArticleApi(id);
            }else{
                mSmartRefreshLayout?.finishLoadMore(0, true, true)
            }
        }
    }




    inner class RefreshHead : OnRefreshListener {

        override fun onRefresh(refreshLayout: RefreshLayout) {

            isPullRefresh = true
            page = 0;
            listContent.clear()
            mainTreeArticleApi(id)
        }
    }


    inner class ContentListAdapter(l : List<IndexArticleListBean>) :
        BaseQuickAdapter<IndexArticleListBean, BaseViewHolder>(R.layout.item_index_layout,l){
        override fun convert(helper: BaseViewHolder, item: IndexArticleListBean) {
            helper.setText(R.id.title,item.title)
                .setText(R.id.tv_subtitle,item.shareUser)
        }
    }



    /**
     * 体系下的文章
     */
    private fun mainTreeArticleApi(id:Int){
        AppData.instance().mainTreeArticleApi(0,id,object : ApiCallBack<IndexArticleListResult> {
            override fun success(data: IndexArticleListResult) {
                println("[mainTreeArticleApi] -> success = ${Gson().toJson(data)}")
                runOnUiThread {
                    pageAll = data.pageCount
                    println("[indexArticleTask] - > success = $pageAll")
                    listContent.addAll(data.datas)
                    contentListAdapter ?.notifyDataSetChanged()
                    if (isPullRefresh) {
                        mSmartRefreshLayout?.finishRefresh()
                    } else {
                        mSmartRefreshLayout?.finishLoadMore(0, true, false)
                    }
                }
            }
            override fun error(error: String) {
                println("[mainTreeArticleApi] -> error = $error")
                runOnUiThread {
                    if (isPullRefresh) {
                        mSmartRefreshLayout?.finishRefresh()
                    } else {
                        mSmartRefreshLayout?.finishLoadMore(0, true, true)
                    }
                }
            }
        })
    }
}